<?php
/*
 *   This file is part of Verbum.
 *
 *   poat is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   poat is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with poat.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * 	 Copyright (c) 2008 Sergio Gabriel Teves <gabriel.sgt at gmail.com>
 */
 
// THESE FUNCTIONS ARE FOR MYSQL DATABASES
class dbmanager {

	var $db;
	var $res;
	var $error;
	var $errno;
	
	var $_host;
	var $_user;
	var $_pass;

	var $logger;

	public function __construct($host, $user, $pass) {
		$this->_host = $host;
		$this->_user = $user;
		$this->_pass = $pass;
		
		$this->logger =& LoggerManager::getLogger('database');
		
	}

	public function testConnect($database) {
		$this->logger->debug("Connecting...");
		$t = mysql_connect($this->_host, $this->_user, $this->_pass);
		if (!$t) {
			$this->error=mysql_error();
			$this->errno=mysql_errno();
			$this->logger->error("Error ".$this->error." [".$this->errno."]");
			return false;
		}
		$t = mysql_select_db($database);
		if (!$t) {
			$this->error=mysql_error();
			$this->errno=mysql_errno();
			$this->logger->error("Error ".$this->error." [".$this->errno."]");
			return false;
		}
		$this->logger->debug("Success");
		return true;
	}

	public function connect($database) {
		$this->logger->debug("Connecting...");
		$db = mysql_connect($this->_host, $this->_user, $this->_pass) or die('Error al conectar a la base de datos. ' . mysql_error());
		mysql_select_db($database) or die("Error al seleccionar la base de datos $database. " . mysql_error());
		$this->db = $db;
		$this->error=mysql_error();
		$this->errno=mysql_errno();
		$this->logger->debug("Success");
	}

	public function query($sql) {
		$this->logger->debug("Query: ".$sql);
		$this->res = mysql_query($sql);
		$this->error=mysql_error();
		$this->errno=mysql_errno();
		if ($this->error!="") $this->logger->error($this->error." [$sql]");		
	}
	
	public function execute($sql) {
		$this->logger->debug("Execute: ".$sql);
		$res = mysql_unbuffered_query($sql);
		$this->res=$res;
		$this->error=mysql_error();
		$this->errno=mysql_errno();
		if ($this->error!="") $this->logger->error($this->error." [$sql]");
		return $res;		
	} 

	public function executeInsert($sql) {
		$this->logger->debug("Insert: ".$sql);
		$res = mysql_unbuffered_query($sql);
		$id = mysql_insert_id();
		$this->res=$res;
		$this->error=mysql_error();
		$this->errno=mysql_errno();
		if ($this->error!="") $this->logger->error($this->error." [$sql]");
		return $id;		
	} 

	public function num_rows() {
		return mysql_num_rows($this->res);
	}

	public function affected_rows() {
		$ret = mysql_affected_rows();
		return $ret;
	}

	public function fetch_array() {
		return mysql_fetch_array($this->res);
	}

	public function fetch_object() {
		return mysql_fetch_object($this->res);
	}

	public function fetch_row() {
		return mysql_fetch_row($this->res);
	}

	public function close() {
		mysql_close($this->db);
	}

	public function exists($table) {
		$this->logger->debug("Exists: ".$table);
		$res = false;
		if (strpos($table,".")!==false) {
			$ap = strpos($table,".");
			$dbn = substr($table,0,$ap);
			$tbn = substr($table,$ap+1);
			$this->logger->debug("Exists DbName: ".$dbn);
			$this->logger->debug("Exists TableName: ".$tbn);
			$q = "SHOW TABLES FROM ".$dbn." LIKE '".$tbn."'";
		} else {
			$q = "SHOW TABLES LIKE '".$table."'";	
		}
		$this->logger->debug("Exists (q): ".$q);
		$res = mysql_num_rows(mysql_query($q));
		$this->logger->debug("Exists (r): ".$res);
		return $res;
	}
}
?>
